package components.game
{
	import UIFree.ObjectEvent;
	import UIFree.controls.uif_gbutton;
	import UIFree.controls.uif_imageButton;
	import UIFree.uif_component;
	import UIFree.uif_ecomponent;
	
	import classes.clt_events;
	
	import components.clt_component;
	
	import data.clt_data;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;

	public class clt_rateLine extends clt_component {
		private const _maxCount: int = 5;
		private var _voteMega: uif_imageButton;
		private var _vote: int;
		private var _buttons: Array;
		private var _cbuttons: uif_ecomponent;
		public function clt_rateLine(parent:uif_component, ax:int=0, ay:int=0, awidth: int=220, aheight:int=50) {
			super(parent, ax, ay, awidth, aheight, 'block');
		}
		
		override protected function createComponents(): void {
			_vote = 0;
			_buttons = new Array();
			var inner: Rectangle = getInner();
			var button: uif_gbutton;
			var size2: Number = inner.width / _maxCount * 1.5;
			var size: Point = new Point((inner.width - size2)/ _maxCount, 20);
			_cbuttons = new uif_ecomponent(this, inner.x, inner.y + (inner.height - size.y) / 2, inner.width, size.y);
			for (var i:int=0; i<_maxCount; i++) {
				button = new uif_gbutton(_cbuttons, i * size.x, 0, size.x, size.y, 'rtbt');
				button.text = '+' + (i + 1).toString();
				button._data = i + 1;
				button.addEvent(MouseEvent.CLICK, doButtonClick);
				_buttons[i] = button;
			}
			
			var dbl: uif_ecomponent = new uif_ecomponent(this, _cbuttons.x + _cbuttons.width - size.x, _cbuttons.y, size.x, size.y);
			button = new uif_gbutton(dbl, 0, 0, size.x, size.y, 'rtbt');
			button.text = 'x2';
			button.hint = clt.sitedata.VOTEDOUBLEHELP;
			button.addEvent(MouseEvent.CLICK, doButton2Click);
			_voteMega = new uif_imageButton(this, clt_servers.IMAGE_URL + 'vote_mega.png', inner.x + inner.width - size2 - 2, 0, doVoteMega);
			_voteMega.image.contentLoaderInfo.addEventListener(Event.COMPLETE, function(e: Event): void {
				_voteMega.y = inner.y + (inner.height - _voteMega.image.height)/2;
			});
			clt.workspace.addEventListener(clt_events.INUSER, function(e: ObjectEvent): void {
				_voteMega.hint = clt.user.voteMega?clt.sitedata.VOTEMEGAHELP1:clt.sitedata.VOTEMEGAHELP2;
			});
		}
		
		public function get vote(): int {
			return _vote;
		}
		
		protected function get buttonVote(): Object {
			return ((vote > 0) && (vote <= _buttons.length))?(_buttons[vote - 1] as uif_gbutton):{styleClass:''};
		}
		
		public function set vote(a_vote: int): void {
			buttonVote.styleClass = 'rtbt';
			_vote 	= a_vote;
			_voteMega.disable = _vote >= clt_data.VOTEMEGA;
			_cbuttons.disable = _vote != 0;
			buttonVote.styleClass = 'rtbt_a';
		}
		
		protected function doVoteMega(e: Event): void {
			if ((vote != clt_data.VOTEMEGA) && clt.user.voteMega) {
				vote = clt_data.VOTEMEGA;
				dispatchEvent(new ObjectEvent(clt_events.VOTE, vote));
			} else clt.workspace.dispatchEvent(new Event(clt_events.VOTEMEGA));
		}
		
		protected function doButtonClick(e: MouseEvent): void {
			if (vote == 0) {
				vote = e.target._data;
				dispatchEvent(new ObjectEvent(clt_events.VOTE, vote));
			}
			
		} 

		protected function doButton2Click(e: MouseEvent): void {
			dispatchEvent(new Event(clt_events.VOTE2));
		} 
	}
}